package com.hk.Mgr.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.kyc.LogReq;
import com.hk.Mgr.web.message.kyc.LogRes;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.StringHelper;
import com.hk.commons.entity.LogUtil;
import com.hk.commons.entity.Organization;
import com.hk.commons.entity.sys.SysUser;
import com.hk.service.IOrganization;
import com.hk.service.kyc.ILogUtil;
import com.hk.service.sys.ISysUser;

/**
 * @author lemon 用户实现类
 */
@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/SysChgLogCtrl")
public class SysChgLogCtrl extends BaseCtrl {

	private Logger logger = LoggerFactory.getLogger(SysChgLogCtrl.class);

	@Autowired
	private ISysUser userService;

	 
	
	@Autowired
	private ILogUtil logService;
 
	@Autowired
	private IOrganization orgService;
	
	 
	/**
	 * @Title: getSysUserList
	 * @Description: 分页获取用户
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/getLogList", method = RequestMethod.POST)
	public ResponseBase<List<LogRes>> getLogList(
			@RequestBody LogReq req) {

		logger.info("getLogList userName={}, curpage={}, pageSize={}", req.getUserName(), req.getCurPage(), req.getPageSize());

		ResponseBase<List<LogRes>> resp = new ResponseBase<List<LogRes>>();
		
		List<LogRes> respList = new ArrayList<LogRes>();
		
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addSql(" where 1=1");
		if (StringUtils.isNotEmpty(req.getChgDesc())) {
			hqlFilter.addLKFilter("chgDesc", req.getChgDesc());
		}

		if (req.getType() != null ) {
			hqlFilter.addEQFilter("type", req.getType());
		}

		if (StringHelper.isNotEmpty(req.getUserName())) {
			String userName = req.getUserName();
			hqlFilter.addSql(" and userId in ( select id from com.hk.commons.entity.sys.SysUser where name  like '%"
					+ userName + "%'  or RealName like '%" + userName + "%')");
		}

		
		
		try {

			if (req.getPageSize() > 30) {
				Long size = logService.countByFilter(hqlFilter);
				resp.setSize(size);
				LogRes logRes = new LogRes();
				respList.add(logRes);
				resp.setResult(respList);
			} else {
				hqlFilter.addSort("cts");
				hqlFilter.addOrder("desc");
				List<LogUtil> retList = logService.findByFilter(hqlFilter,
						req.getCurPage(), req.getPageSize());
				if (retList != null && !retList.isEmpty()) {
					
					for (LogUtil log : retList) {
						LogRes logRes = new LogRes();
						ReqToModelHelper.copyReqValueToModel(log, logRes);
						
						SysUser user = new SysUser();
						
						if(log.getUserId() != null){
							user = userService.findById(log.getUserId());
							if(user != null && StringHelper.isNotEmpty(user.getRealName())){
								logRes.setUserName(user.getRealName());
							}
						}
						
						if(log.getActUserId() != null){
							SysUser chgUser = userService.findById(log.getActUserId());
							
							// 添加修改者全名
							if(chgUser != null && StringHelper.isNotEmpty(chgUser.getRealName())){
								logRes.setActUserName( chgUser.getRealName());
							}
 
							// 添加修改者部门
							if (chgUser != null && chgUser.getOrgId() != null) {
								Organization org = orgService.findById(chgUser
										.getOrgId());
								if (org != null) {
									String depFullName = org.getFlag02();
									if (StringHelper.isNotEmpty(depFullName)) {
										logRes.setActUserDepFullName(depFullName);
									}
								}
							}
							
						}
						respList.add(logRes);
					}

					Long total = logService.countByFilter(hqlFilter);
					resp.setSize(total);
				}
				resp.setResult(respList);
				resp.setResultCode(StatusCode.SUCCESS.key());
			}
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}

		return resp;
	}

	 
}
